<?php

class ExcelController extends RController
{
        public function filters() {    
            return array('rights',);             
        }  
        
        public function allowedActions(){
            return 'index';             
       } 
        public function actionFecha()
       {
            if(isset($_POST['fechaInicio'])&&isset($_POST['fechaFin'])&&isset($_POST['id'])&&isset($_POST['tipo']))
                {
                $tipo = $_POST['tipo'];
                $id = $_POST['id'];
                 $arrInicio = explode('/', $_POST['fechaInicio']); 
		$fechaInicio = $arrInicio[2].'-'.$arrInicio[1].'-'.$arrInicio[0]; 
			
			$arrFin = explode('/', $_POST['fechaFin']); 
			$fechaFin = $arrFin[2].'-'.$arrFin[1].'-'.$arrFin[0]; 
			
			$connection = Yii::app()->db;
                    if($tipo == 'Departamento')
                        {
                             $sqlScript = "call reporte_general_departamento('".$fechaInicio." 00:00:00','".$fechaFin." 23:59:59','".$id."');";
                             $command = $connection->createCommand($sqlScript);
			
			$dataReader = $command->query();		
			
			$rows=$dataReader->readAll();				
			
			$connection->active=false;
			
			
			$dataProvider2=new CSqlDataProvider($sqlScript, array(
			    'totalItemCount'=>count($rows),
			    'pagination'=>array(
			       'pageSize'=>10,
			    ),
			));
			
                        $dataProvider = $dataProvider2->getData();
			$this->render('resultado',array('dataProvider'=>$dataProvider));
                   
              
                             
                        }
                    else if($tipo == 'Usuario')
                        {
                        $sqlScript = "call reporte_general_usuario('".$fechaInicio." 00:00:00','".$fechaFin." 23:59:59','".$id."');";
                        $command = $connection->createCommand($sqlScript);
			
			$dataReader = $command->query();		
			
			$rows=$dataReader->readAll();				
			
			$connection->active=false;
			
			
			$dataProvider2=new CSqlDataProvider($sqlScript, array(
			    'totalItemCount'=>count($rows),
			    'pagination'=>array(
			       'pageSize'=>10,
			    ),
			));
			
                        $dataProvider = $dataProvider2->getData();
			$this->render('resultado',array('dataProvider'=>$dataProvider));
                   
              
                        
                        }
                        else if($tipo == 'Flujo')
                            {
                            
                            $name = $_POST['name'];
                            $sqlScript = "call reporte_general_estado('".$fechaInicio." 00:00:00','".$fechaFin." 23:59:59','".$name."');";
                        $command = $connection->createCommand($sqlScript);
			
			$dataReader = $command->query();		
			
			$rows=$dataReader->readAll();				
			
			$connection->active=false;
			
			
			$dataProvider2=new CSqlDataProvider($sqlScript, array(
			    'totalItemCount'=>count($rows),
			    'pagination'=>array(
			       'pageSize'=>10,
			    ),
			));
			
                        $dataProvider = $dataProvider2->getData();
			$this->render('ResultadoFlujo',array('dataProvider'=>$dataProvider));
                            }
               
                
                }
                else if(isset($_GET['id']))
                {
                    if(isset($_GET['name']))
                        {
                        $this->render('Fecha',array('id'=>$_GET['id'],'tipo'=>$_GET['tipo'],'name'=>$_GET['name']));
                        }
                        else{
                    $this->render('Fecha',array('id'=>$_GET['id'],'tipo'=>$_GET['tipo']));
                        }
                }
          
                
            
       }
       
 
	public function actionDocumentoDepartamento()
	{
           
            $model=new Departamentos('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Departamentos']))
			$model->attributes=$_GET['Departamentos'];

		$this->render('DocumentoDepartamento',array(
			'model'=>$model,
		));
                
	}

	public function actionDocumentoFlujo()
	{
            $model=new Flujos('search');
            $model->unsetAttributes();  // clear any default values
            if(isset($_GET['Flujos']))
                $model->attributes=$_GET['Flujos'];

            $this->render('DocumentoFlujo',array(
            'model'=>$model,
        ));		
	}

	public function actionDocumentoPrioridad()
	{
            if(isset($_POST['fechaInicio'])&&isset($_POST['fechaFin']))
                {
                        $arrInicio = explode('/', $_POST['fechaInicio']); 
			$fechaInicio = $arrInicio[2].'-'.$arrInicio[1].'-'.$arrInicio[0]; 
			
			$arrFin = explode('/', $_POST['fechaFin']); 
			$fechaFin = $arrFin[2].'-'.$arrFin[1].'-'.$arrFin[0]; 
			
			$connection = Yii::app()->db;
		
                        $Prioridad='';
           
                    if(isset($_POST['alta']))
                        {
                        $Prioridad='Alta';
                        $sqlScript = "call reporte_general_prioridad('".$fechaInicio." 00:00:00','".$fechaFin." 23:59:59','Alta');";
                       
                        }
                    else if (isset($_POST['media']))
                        {
                        $Prioridad='Media';
                           $sqlScript = "call reporte_general_prioridad('".$fechaInicio." 00:00:00','".$fechaFin." 23:59:59','Media');";
                    }
                    elseif($_POST['baja'])
                        {
                        $Prioridad='Baja';
                           $sqlScript = "call reporte_general_prioridad('".$fechaInicio." 00:00:00','".$fechaFin." 23:59:59','Baja');";
                        }
                        
                        $command = $connection->createCommand($sqlScript);
			
			$dataReader = $command->query();		
			
			$rows=$dataReader->readAll();				
			
			$connection->active=false;
			
			
			$dataProvider2=new CSqlDataProvider($sqlScript, array(
			    'totalItemCount'=>count($rows),
			    'pagination'=>array(
			       'pageSize'=>10,
			    ),
			));
			
		$dataProvider = $dataProvider2->getData();
			$this->render('ResultadoEstado',array('dataProvider'=>$dataProvider,'Prioridad'=>$Prioridad));
                   
              
               }
               else
               {
		$this->render('DocumentoPrioridad');
               }
	}

	public function actionDocumentoUsuario()
	{
            $model=new Usuarios('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Usuarios']))
            $model->attributes=$_GET['Usuarios'];

        $this->render('DocumentoUsuario',array(
            'model'=>$model,
        ));
		
	}

	public function actionGeneral()
	{
            if(isset($_POST['fechaInicio'])&&isset($_POST['fechaFin']))
                {
                
			$arrInicio = explode('/', $_POST['fechaInicio']); 
			$fechaInicio = $arrInicio[2].'-'.$arrInicio[1].'-'.$arrInicio[0]; 
			
			$arrFin = explode('/', $_POST['fechaFin']); 
			$fechaFin = $arrFin[2].'-'.$arrFin[1].'-'.$arrFin[0]; 
			
					

			
			$connection = Yii::app()->db;
			 
			$sqlScript = "call reporte_general('".$fechaInicio." 00:00:00','".$fechaFin." 23:59:59');";

                    
                
                        //$sqlScript = "call reporte_general('".$fechaInicio." 00:00:00','".$fechaFin." 23:59:59');";
                        
                        
                        
			$command = $connection->createCommand($sqlScript);
			
			$dataReader = $command->query();		
			
			$rows=$dataReader->readAll();				
			
			$connection->active=false;
			
			//$sql='call reporte_general;';
			
			$dataProvider2=new CSqlDataProvider($sqlScript, array(
			    'totalItemCount'=>count($rows),
			   /* 'sort'=>array(
			        'attributes'=>array(
			             'id', 'username', 'email',
			        ),
			    ),*/
			    'pagination'=>array(
			       'pageSize'=>10,
			    ),
			));
			
		$dataProvider = $dataProvider2->getData();
			$this->render('resultado',array('dataProvider'=>$dataProvider));
                    }
                    else
                    {
           
		$this->render('General');
                    }
	}

	public function actionIndex()
	{
		$this->render('index');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}